﻿using System;
using System.Diagnostics;
using System.Reflection;
using System.Xml;
using YioksPlanEditor.ObjectCopy;
using YioksPlanEditor.Tools;

namespace YioksPlanEditor.Models
{
    public class SoftVersionInfo : Model
    {
        public string FormVersionX86 { get; set; }
        public string VersionX86 { get; set; }
        public string FormVersionX64 { get; set; }
        public string VersionX64 { get; set; }

        public bool IsUpdate { get; set; }

        public SoftVersionInfo()
        {
            if (YioksTool.IsX86)
            {
                SetDefaultProp("FormVersionX86", VersionUtil.AssemblyFileVersion.ToString());
                SetDefaultProp("FormVersionX64", "1.0.0.0");
            }
            else
            {
                SetDefaultProp("FormVersionX64", VersionUtil.AssemblyFileVersion.ToString());
                SetDefaultProp("FormVersionX86", "1.0.0.0");
            }

            SetDefaultProp("VersionX86", "1.0.0.0");
            SetDefaultProp("VersionX64", "1.0.0.0");
            SetDefaultProp("IsUpdate", false);
        }

        public string SetFormVersion(string val)
        {
            return YioksTool.IsX86 ? FormVersionX86 = val : FormVersionX64 = val;
        }

        public string SetSubVersion(string val)
        {
            return YioksTool.IsX86 ? VersionX86 = val : VersionX64 = val;
        }

        public string GetFormVersion()
        {
            return YioksTool.IsX86 ? FormVersionX86 : FormVersionX64;
        }

        public string GetSubVersion()
        {
            return YioksTool.IsX86 ? VersionX86 : VersionX64;
        }

        #region 解析XML
        public void ParseCfgXml(string xmlPath)
        {
            var xmlInfo = FileTool.File_Read(xmlPath).Trim();
            if (string.IsNullOrEmpty(xmlInfo) || xmlInfo.Contains("ERROR:")) return;
            xmlInfo = xmlInfo.Replace("SoftVersionInfo", "SoftVersionInfoXml");
            var xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(xmlInfo);
            var root = xmlDocument.DocumentElement;

            var subSoftCfgXml = XmlTool.Deserialize(typeof(SoftVersionInfoXml), root) as SoftVersionInfoXml;
            if (subSoftCfgXml != null)
            {
            }
            else
            {
                subSoftCfgXml = new SoftVersionInfoXml();
            }

            VersionX86 = subSoftCfgXml.VersionX86;
            FormVersionX86 = subSoftCfgXml.FormVersionX86;
            VersionX64 = subSoftCfgXml.VersionX64;
            FormVersionX64 = subSoftCfgXml.FormVersionX64;
            IsUpdate = bool.Parse(subSoftCfgXml.IsUpdate);
        }
        #endregion

        #region 导出XML
        /// <summary>
        /// 导出XML
        /// </summary>
        public string ExportXml(string xmlPath)
        {
            var xmlMainDocument = new XmlDocument();
            var xmlDeclaration = xmlMainDocument.CreateXmlDeclaration("1.0", "utf-8", "yes");
            xmlMainDocument.AppendChild(xmlDeclaration);
            var rootNode = xmlMainDocument.AppendChild(_getXmlNode(XmlUtil.GetObjXml(this), xmlMainDocument));
            xmlMainDocument.AppendChild(rootNode);
            xmlMainDocument.Save(xmlPath);
            return xmlMainDocument.OuterXml;
        }

        private static XmlNode _getXmlNode(string xmlInfo, XmlDocument xmlMainDocument)
        {
            var xmlItemDocument = new XmlDocument();
            xmlItemDocument.LoadXml(xmlInfo);
            if (xmlItemDocument.DocumentElement != null)
            {
                var xmlNode = xmlMainDocument.ImportNode(xmlItemDocument.DocumentElement.FirstChild, true);
                var uuidNode = xmlNode.SelectSingleNode("_myid");
                if (uuidNode != null)
                    xmlNode.RemoveChild(uuidNode);
                return xmlNode;
            }
            return null;
        }
        #endregion
    }

    public class SoftVersionInfoXml : Model
    {
        public string FormVersionX86 { get; set; }
        public string VersionX86 { get; set; }
        public string FormVersionX64 { get; set; }
        public string VersionX64 { get; set; }

        public string IsUpdate { get; set; }
        public SoftVersionInfoXml()
        {
            if (YioksTool.IsX86)
            {
                SetDefaultProp("FormVersionX86", VersionUtil.AssemblyFileVersion.ToString());
                SetDefaultProp("FormVersionX64", "1.0.0.0");
            }
            else
            {
                SetDefaultProp("FormVersionX64", VersionUtil.AssemblyFileVersion.ToString());
                SetDefaultProp("FormVersionX86", "1.0.0.0");
            }

            SetDefaultProp("VersionX64", "1.0.0.0");
            SetDefaultProp("FormVersionX64", "1.0.0.0");
            SetDefaultProp("IsUpdate", "true");
        }
    }

    public class VersionUtil
    {
        public static Version AssemblyVersion => Assembly.GetExecutingAssembly().GetName().Version;

        public static Version AssemblyFileVersion => new Version(FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion);

        public static string AssemblyInformationalVersion => FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion;
    }
}
